﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using GroceryPriceBook.Core.Service;
using GroceryPriceBook.Core.ViewModel;
using GroceryPriceBook.Core;
using GroceryPriceBook.Extensions;

namespace GroceryPriceBook.Controllers
{
	public class PurchaseLogController : BaseController
	{
		readonly PurchaseLogHandler _handler = new PurchaseLogHandler();
		readonly StoreHandler _handlerStore = new StoreHandler();
		readonly ProductHandler _handlerProduct = new ProductHandler();

		// GET: /PurchaseLog/
		public ActionResult Index()
		{
			return View(_handler.ReadAll());
		}

		// GET: /PurchaseLog/Details/5
		public ActionResult Details(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var purchaseLog = _handler.ReadOne(id.Value);
			if (purchaseLog == null)
			{
				return HttpNotFound();
			}
			return View(purchaseLog);
		}

		// GET: /PurchaseLog/Create
		public ActionResult Create()
		{
			var purchaseLog = new PurchaseLog
							  {
								  StoreSelectListItems =
									  _handlerStore.ReadAll()
												   .ToSelectListItems(store => store.Name,
																	  store => store.Id.ToString(),
																	  store => store.Id == Guid.Empty),
								  PurchaseLogItems = new List<PurchaseLogItem>()
							  };
			purchaseLog.PurchaseLogItems.Add(new PurchaseLogItem()
											 {
												 ProductSelectListItems = _handlerProduct.ReadAll()
													.ToSelectListItems(product => product.Name,
																	   product => product.Id.ToString(),
																	   product => product.Id == Guid.Empty)
											 }
				);
			ViewBag.ProductSelectList = _handlerProduct.ReadAll().ToSelectListItems(product => product.Name,
													   product => product.Id.ToString(),
													   product => product.Id == Guid.Empty);

			return View(purchaseLog);
		}

		// POST: /PurchaseLog/Create
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create(PurchaseLog purchaseLog)
		{
			if (ModelState.IsValid)
			{
				if (_handler.Create(ref purchaseLog) == ServiceResultEnum.Success)
				{
					return RedirectToAction("Index");
				}
			}

			purchaseLog.StoreSelectListItems =
				_handlerStore.ReadAll()
							 .ToSelectListItems(store => store.Name,
												store => store.Id.ToString(),
												store => store.Id == purchaseLog.StoreId);
			var products = _handlerProduct.ReadAll();
			ViewBag.ProductSelectList = products.ToSelectListItems(product => product.Name,
																   product => product.Id.ToString(),
																   product => product.Id == Guid.Empty);
			for (var index = 0; index < purchaseLog.PurchaseLogItems.Count; index++)
			{
				var item = purchaseLog.PurchaseLogItems[index];
				item.ProductSelectListItems = products
					.ToSelectListItems(product => product.Name,
									   product => product.Id.ToString(),
									   product => product.Id == item.ProductId);
			}

			return View(purchaseLog);
		}

		// GET: /PurchaseLog/Edit/5
		public ActionResult Edit(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var purchaseLog = _handler.ReadOne(id.Value);
			if (purchaseLog == null)
			{
				return HttpNotFound();
			}

			// populate store drop down and select the store previously selected
			purchaseLog.StoreSelectListItems =
				_handlerStore.ReadAll()
							 .ToSelectListItems(store => store.Name,
												store => store.Id.ToString(),
												store => store.Id == purchaseLog.StoreId);

			// get products from database
			var products = _handlerProduct.ReadAll();

			// populate product drop down list 
			ViewBag.ProductSelectList = products.ToSelectListItems(product => product.Name,
																   product => product.Id.ToString(),
																   product => product.Id == Guid.Empty);

			// for reach purchase item populate items
			for (var index = 0; index < purchaseLog.PurchaseLogItems.Count; index++)
			{
				var item = purchaseLog.PurchaseLogItems[index];
				item.ProductSelectListItems = products
					.ToSelectListItems(product => product.Name,
									   product => product.Id.ToString(),
									   product => product.Id == item.ProductId);

				// calculate total price
				item.Price = item.Quantity * item.PricePerItem;

				// set the product values
				item.Product = products.Single(product => product.Id == item.ProductId);
			}

			return View(purchaseLog);
		}

		// POST: /PurchaseLog/Edit/5
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Edit(PurchaseLog purchaseLog)
		{
			if (ModelState.IsValid)
			{
				if (_handler.Update(ref purchaseLog) == ServiceResultEnum.Success)
				{
					return RedirectToAction("Index");
				}
			}

			purchaseLog.StoreSelectListItems =
				_handlerStore.ReadAll()
							 .ToSelectListItems(store => store.Name,
												store => store.Id.ToString(),
												store => store.Id == purchaseLog.StoreId);
			var products = _handlerProduct.ReadAll();
			ViewBag.ProductSelectList = products.ToSelectListItems(product => product.Name,
																   product => product.Id.ToString(),
																   product => product.Id == Guid.Empty);
			for (var index = 0; index < purchaseLog.PurchaseLogItems.Count; index++)
			{
				var item = purchaseLog.PurchaseLogItems[index];
				item.ProductSelectListItems = products
					.ToSelectListItems(product => product.Name,
									   product => product.Id.ToString(),
									   product => product.Id == item.ProductId);
			}
			return View(purchaseLog);
		}

		// GET: /PurchaseLog/Delete/5
		public ActionResult Delete(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var purchaseLog = _handler.ReadOne(id.Value);
			if (purchaseLog == null)
			{
				return HttpNotFound();
			}
			return View(purchaseLog);
		}

		// POST: /PurchaseLog/Delete/5
		[HttpPost, ActionName("Delete")]
		[ValidateAntiForgeryToken]
		public ActionResult DeleteConfirmed(Guid id)
		{
			if (_handler.Delete(id) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}
			return View(_handler.ReadOne(id));
		}
	}
}
